﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Objects;
using ServicesComponent.Repositories;

public partial class QuanLyHocSinh_PhanLopDetails : System.Web.UI.Page
{
    private IPhanLopRepository _PhanLopRepository;
    private IClassRepository _ClassRepository;
    private IStudentRepository _StudentRepository;
    private static string id = "";
    public  int Stt;
    private static string action;
    protected void Page_Load(object sender, EventArgs e)
    {
        Stt = Stt + 1;
        _PhanLopRepository = FactoryServices.GetServicesPhanLop(_PhanLopRepository);
        action = Request.QueryString["ac"].ToString();
        id = Request.QueryString["id"].ToString();
        if (IsPostBack == false)
        {
          
            if (action == "'sua'")
            {
                GetDataToComboboxEdit();
                PhanLop obj = _PhanLopRepository.Get(id);
                ddlKhoi.SelectedValue = obj.ClassID.ToString();
               // ddlKhoi.SelectedValue = id;
                txtNgayVao.Text = obj.StartDate.ToShortDateString();
                txtNgayRa.Text = obj.Endate.ToShortDateString();
                ddlKhoi.Enabled = false;
                txtNgayVao.Attributes.Add("readonly", "readonly");
                txtNgayRa.Attributes.Add("readonly", "readonly");
                txtNgayVao.Enabled = false;
                txtNgayRa.Enabled = false;
                GetDataRepeatEdit(ddlKhoi.SelectedValue);

            }
            if (action == "'them'")
            {
             //   lblTitle.Text = "Thêm mới niên khóa";
                GetDataToComboboxAdd();
                GetDataRepeat();
            }
        }
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (action == "'sua'")
        {
            RepeaterItem ri = e.Item;

            var isDefaultCheckBox = ri.FindControl("ChBox") as CheckBox; 
            var isInClass = ri.FindControl("HiddenField1") as HiddenField;
            if (isDefaultCheckBox != null)
            {
                if (isInClass != null && isInClass.Value=="True")
                {
                    
              
                    isDefaultCheckBox.Checked = true;
                }
            else
                {
                    isDefaultCheckBox.Checked = false;
                }
            }
           
        }
    }
    private void GetDataToComboboxAdd()
    {
        _ClassRepository = FactoryServices.GetServicesClass(_ClassRepository);
        ddlKhoi.DataSource = _ClassRepository.GetClassBySchoolIdAndSchoolYearIDNotPhanLop(int.Parse(Session["SchoolID"].ToString()), Session["SchoolYearID"].ToString());
        ddlKhoi.DataValueField = "Id";
        ddlKhoi.DataTextField = "Name";
        ddlKhoi.DataBind();


    }
    private void GetDataToComboboxEdit() 
    {
        _ClassRepository = FactoryServices.GetServicesClass(_ClassRepository);
        ddlKhoi.DataSource = _ClassRepository.GetList();
        ddlKhoi.DataValueField = "Id";
        ddlKhoi.DataTextField = "Name";
        ddlKhoi.DataBind();


    }
    //Get edit
    private void GetDataRepeatEdit(string classId)
    {
        _StudentRepository = FactoryServices.GetServicesStudent(_StudentRepository);
      List<Student> list1 = _StudentRepository.GetStudentBySchoolIdAndSchoolYearClass(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()),classId);

        foreach (var student in list1)
        {
            student.Unrefuge = true;
        }
        List<Student> list =  _StudentRepository.GetStudentBySchoolIdAndSchoolYearNotClass(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()));
        foreach (var student in list)
        {
            student.Unrefuge = false;
            list1.Add(student);
        }
        rptA.DataSource = list1;
        rptA.DataBind();

        Cpage.DataSource = list1;
        Cpage.BindToControl = rptA;
        rptA.DataSource = Cpage.DataSourcePaged;
    }
    //get add - ClassID luôn là duy nhất
    void GetDataRepeat()
    {
        _StudentRepository = FactoryServices.GetServicesStudent(_StudentRepository);
        rptA.DataSource = _StudentRepository.GetStudentBySchoolIdAndSchoolYearNotClass(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()));

        rptA.DataBind();

        Cpage.DataSource = _StudentRepository.GetStudentBySchoolIdAndSchoolYearNotClass(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()));
        Cpage.BindToControl = rptA;
        rptA.DataSource = Cpage.DataSourcePaged;
    }
    protected void ddlKhoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlKhoi.SelectedIndex == 0)
        {
          //  bind();
        }
        else
        {
         //   bind2();
        }
    }
    bool ValiDate()
    {
        try
        {
            DateTime.Parse(txtNgayRa.Text);
            DateTime.Parse(txtNgayVao.Text);
        }
        catch (Exception)
        {
            Alert.Show("Chưa đúng định dạng ngày/tháng/năm !");
            if ((action == "'them'"))
            {
                GetDataRepeat();
            }
            else
            {
                GetDataRepeatEdit(ddlKhoi.SelectedValue);
            }
           // Alert.ClodePopup();
            return false;
        }
        //if (txtMaNK.Value.ToString() == "" || txtTenNK.Value.ToString() == "" || txtNamNK.Value.ToString() == "")
        //{
        //    return false;
        //}
        return true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        if (ValiDate())
        {

            if (action == "'sua'")
            {
                foreach (RepeaterItem item in rptA.Items)
                {
                    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                    HiddenField oField = item.FindControl("HdField") as HiddenField;
                    if (!ch.Checked)
                    { //Edit
                      //  Guid idStu = Guid.Parse(oField.Value.ToString());
                        if (oField != null) _PhanLopRepository.DeleteStudentFromPhanLop(oField.Value);
                        // _PhanLopRepository.Delete(new PhanLop(Guid.Parse(id), Guid.Parse(ddlKhoi.SelectedValue.ToString()), idStu, Guid.Parse(Session["SchoolYearID"].ToString()), Convert.ToDateTime(txtNgayVao.Value.ToString()), Convert.ToDateTime(txtNgayRa.Value.ToString())));
                       //// ch.Text = "IT is selected now";
                    }
                }
 
                Alert.Show("Cập nhật dữ liệu thành công!");
                Alert.ClodePopup();


            }
            if (action == "'them'")
            {
                foreach (RepeaterItem item in rptA.Items)
                {
                    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                    HiddenField oField = item.FindControl("HdField") as HiddenField;
                    if (ch.Checked)
                    { //AddNew
                        Guid idStu = Guid.Parse(oField.Value.ToString());
                        _PhanLopRepository.Insert(new PhanLop(Guid.NewGuid(), Guid.Parse(ddlKhoi.SelectedValue.ToString()), idStu, Guid.Parse(Session["SchoolYearID"].ToString()), Convert.ToDateTime(txtNgayVao.Text.ToString()), Convert.ToDateTime(txtNgayRa.Text.ToString())));
                        ch.Text = "IT is selected now";
                    }
                }
               
                Alert.Show("Thêm mới dữ liệu thành công!");
                Alert.ClodePopup();
            }
        }
        else
        {
            Alert.Show("Thông tin cần nhập chưa đầy đủ !");
        }
 
    }

}